package com.focusai.efairy.ui.adapter.project;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.project.ProjectItem;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by luo_guangwen on 2017/9/8.
 */

public class ProjectInfoTypeViewBinder extends ItemViewBinder<ProjectItem, ProjectInfoTypeViewBinder.ViewHolder> {

    private Context mContext;
    private String userName = "";
    private OnItemClickListener onItemClickListener;
    private String userId;

    public ProjectInfoTypeViewBinder(Context mContext) {
        this.mContext = mContext;
        this.userId = CacheManager.getUserId();
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_project_info, parent, false);
        userName = CacheManager.getUserName();
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final ProjectItem dataItem) {
        if (!String.valueOf(dataItem.efairyproject_creator_id).equals(userId)) {
            holder.tvInfoManager.setVisibility(View.GONE);
            holder.layoutTitle.setOnClickListener(null);
        } else {
            holder.tvInfoManager.setVisibility(View.VISIBLE);
            if (this.onItemClickListener != null) {
                holder.layoutTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(dataItem);
                    }
                });
            }
        }
        holder.tvLeaderName.setText(dataItem.efairyproject_user_name);
        holder.tvLeaderPhone.setText(dataItem.efairyproject_user_phonenumber);
        holder.tvProjectName.setText(dataItem.efairyproject_name);
        holder.tvProjectAddress.setText(dataItem.efairyproject_address);
        holder.tvProjectDesc.setText(dataItem.efairyproject_description);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvInfoManager;
        TextView tvLeaderName;
        TextView tvLeaderPhone;
        TextView tvProjectName;
        TextView tvProjectAddress;
        TextView tvProjectDesc;
        LinearLayout layoutTitle;

        ViewHolder(View itemView) {
            super(itemView);
            tvInfoManager = (TextView) itemView.findViewById(R.id.text_project_manager);
            tvLeaderName = (TextView) itemView.findViewById(R.id.text_leader_name);
            tvLeaderPhone = (TextView) itemView.findViewById(R.id.text_leader_phone);
            tvProjectName = (TextView) itemView.findViewById(R.id.text_project_name);
            tvProjectAddress = (TextView) itemView.findViewById(R.id.text_project_address);
            tvProjectDesc = (TextView) itemView.findViewById(R.id.text_project_desc);
            layoutTitle = (LinearLayout) itemView.findViewById(R.id.llayout_title);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(ProjectItem item);
    }
}
